var serverPort = "5002";
var serverApi = "https://xhe.dhcloud123.com/a02";//https://xcx.shoubaoyiliao.com/a02
var serverApi3 = "https://xhe.dhcloud123.com/a03";//https://xcx.shoubaoyiliao.com/a03
var serverApi1 = "https://xhe.dhcloud123.com/a01";
let host = "https://xhe.dhcloud123.com";
var socketUrl6 = "wss://xhe.dhcloud123.com/wss/websocket";//ws://192.168.0.200:9106/websocket
var txRtcId = "1400375385";//医健云联腾讯云RTC的appid
var serverIP = "139.186.194.136";


//首保
// let host = "https://xcx.shoubaoyiliao.com";
// var socketUrl6 = "wss://xcx.shoubaoyiliao.com/wss/websocket";
// var txRtcId = "1400408139";//首保腾讯云RTC的appid

//医健云联
// var serverPort = "5002";
// var serverApi = "https://xhe.dhcloud123.com/a02";//https://xcx.shoubaoyiliao.com/a02
// var serverApi3 = "https://xhe.dhcloud123.com/a03";//https://xcx.shoubaoyiliao.com/a03
// var serverApi1 = "https://xhe.dhcloud123.com/a01";
// let host = "https://xhe.dhcloud123.com";
// var socketUrl6 = "wss://xhe.dhcloud123.com/wss/websocket";//ws://192.168.0.200:9106/websocket
// var txRtcId = "1400375385";//医健云联腾讯云RTC的appid
// var serverIP = "139.186.194.136";

var desKey1 = "he28034";
var desKey2 = "hua2938";
var desKey3 = "jk923049";

var ringLai = new Audio("audio/ringLai.wav");
var ringLaiTw = new Audio("audio/ringLaiTw.wav");
//格式化参数，把json对象转为 id=xxx&name=xxx 格式
function formatParams(data) {
    var arr = [];
    for (var name in data) {
        arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
    }
    //arr.push(('v='Math.random()).replace('.',''));
    return arr.join('&');
}


function getUrl(path) {
    return host + path;
}

function isNullOrEmpty(text) {
    if (!text || text == '') {
        return true;
    }
    return false;
}

function post(path, data, success = function (res) {
}, error = function (e) {
}) {
    console.log(path + " request: " + JSON.stringify(data));
    $.ajax({
        url: getUrl(path),
        type: 'post',
        dataType: 'json',
        data: data,
        headers: {
            "Content-Type": 'application/x-www-form-urlencoded',
            "Content-Length": data.length
        },
        success: success,
        error: error
    });
}

function get(path, data, success = function (res) {
}, error = function (e) {
}) {
    console.log(path + " request: " + JSON.stringify(data));
    $.ajax({
        url: getUrl(path),
        type: 'get',
        dataType: 'json',
        data: data,
        headers: {
            "Content-Type": 'application/x-www-form-urlencoded',
            "Content-Length": data.length
        },
        success: success,
        error: error
    });
}

/**
 * 格式化填充数据，在html 中以 {name} 的形式预留桩位
 * @param {Object} dta
 * @param {Object} tmpl
 */
function formatTemplate(dta, tmpl) {
    var format = {
        name: function (x) {
            return x;
        }
    };
    return tmpl.replace(/{(\w+)}/g, function (m1, m2) {
        if (!m2)
            return "";
        return (format && format[m2]) ? format[m2](dta[m2]) : dta[m2];
    });
}

//发送事件：1发服务包2发处方3发医生视频链接
function submitMemberEvent(glId, type, types, doctorId) {
    var reqData = {doctorId: doctorId, memberId: memberId, glId: glId, type: type, types: types};
    $.ajax({
        type: "POST",
        dataType: "JSON",
        data: reqData,
        contentType: "application/x-www-form-urlencoded",
        url: serverApi3 + "/umsmembernews/addUmsMemberNews" + "?" + formatParams(reqData),
        success: function (result) {

        },
        error: function (e) {
            console.error(JSON.stringify(e));
        }
    })
}

/*检查数字*/
function shuziCheck(s) {
    var cuo = 0;
    var zhengzhengshu = /(^[0-9]\d*$)/;//正整数
    var fudianshu = /^[0-9]+.?[0-9]*/; //浮点数
    if (s == "") {
        cuo = 1;
    } else if (s.indexOf("-") != -1) {
        cuo = 1;
    }//不是正数
    else if (!(fudianshu.test(s))) {
        cuo = 1;
    }//不是浮点数或数字
    else if (s.indexOf(".") != -1 && s.substring(s.indexOf(".") + 1, s.length).length > 1) {
        cuo = 1;
    }//最多1位小数
    else if (s * 1 > 1000) {
        cuo = 1;
    }//不能超过1000
    if (cuo > 0) {
        tishick("数字格式错误，需大于0且最多1位小数");
    }
    return cuo;
}

/********************************************截取图片*******************************************/
var mainwh = "0";
var mainhg = "0";

function doScreenShot2() {
    var dy = $("#cation-tbale")[0];
    html2canvas(dy, {
        onrendered: function (canvas) {
            canvas.id = "mycanvas";
            mainwh = 560; //$("#h5frame").width()
            mainhg = 560; //$("#h5frame").height()
            var img = convertCanvasToImage(canvas);
            //console.log(img);
            $("#dw").html(img) //添加到展示图片div区域
            img.onload = function () {
                img.onload = null;
                canvas = convertImageToCanvas(img, 0, 0, mainwh, mainhg); //设置图片大小和位置
                img.src = convertCanvasToImage(canvas).src;
                $(img).css({background: "#FAFAFA"});
                //调用下载方法
                download(img.src)
            }
        }
    });
}

//绘制显示图片
function convertCanvasToImage(canvas) {
    var image = new Image();
    image.src = canvas.toDataURL("image/png"); //获得图片地址
    return image;
}

//生成canvas元素，相当于做了一个装相片的框架
function convertImageToCanvas(image, startX, startY, width, height) {
    var canvas = document.createElement("canvas");
    canvas.width = width * 2.77857;
    canvas.height = height * 2.77857;
    //在这调整图片中内容的显示（大小,放大缩小,位置等）
    canvas.getContext("2d").imageSmoothingEnabled = false;
    canvas.getContext("2d").drawImage(image, startX, startY, width, height, 0, 0, mainwh * 2.77857, mainhg * 2.77857);
    //canvas.getContext("2d").drawImage(img,0,0,mainwh*2.77857,mainhg*2.77857);
    return canvas;
}

function DoSaveAsIMG() {
    if (document.all.IframeReportImg.src != "about:blank")
        window.frames["IframeReportImg"].document.execCommand("SaveAs");
}

// 另存为图片
function download(src) {
    var now = getTime();
    var $a = $("<a></a>").attr("href", src).attr("download", now + ".png");
    $a[0].click();
}

//时间格式化
function getTime() {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    //这样写显示时间在1~9会挤占空间；所以要在1~9的数字前补零;
    if (hour < 10) {
        hour = '0' + hour;
    }
    if (minute < 10) {
        minute = '0' + minute;
    }
    if (second < 10) {
        second = '0' + second;
    }
    //var x=date.getDay();//获取星期
    var time = year + '.' + month + '.' + day + '_' + hour + '.' + minute + '.' + second
    return time;
}

/********************************************截取图片*******************************************/